package com.zqh.synchronizedDemo;

import java.util.concurrent.TimeUnit;

/**
 * @author 曾庆红
 * {@code @date} 2023-10-11 20:44
 * 异常锁
 * 程序在执行过程中，如果出现异常，默认情况下锁会被释放
 * 所以，在并发处理过程中，有异常要小心，不然容易出现不一致的情况
 */
public class T5 {
    int count = 0;
    synchronized void m(){
        System.out.println(Thread.currentThread().getName()+" start");
        while(true){
            count++;
            System.out.println(Thread.currentThread().getName()+" count="+count);
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if(count == 5){
                int i = 1/0;
                System.out.println(i);
            }
        }
    }

    public static void main(String[] args) {
        T5 t =new T5();
        Runnable r = ()->{
            t.m();
        };
        new Thread(r,"t1").start();
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        new Thread(r,"t2").start();
    }
}
